<?php
/**
 * Created by PhpStorm.
 * User: wo
 * Date: 2019/9/10
 * Time: 20:06
 */
class Hot_themeApp extends BackendApp
{
    var $_hotTheme_mod;
    var $_theme_mod;

    function __construct()
    {
        $this->Banner_settingApp();
    }

    function Banner_settingApp()
    {
        parent::BackendApp();
        $this->_hotTheme_mod =& m('hot_theme');
        $this->_theme_mod =& m('theme_setting');
    }
    /***热门主题列表**/
    function index(){
        $page = $this->_get_page();
        $res = $this->_hotTheme_mod->find(array(
            'limit' => $page['limit'],
            'count' => true,
            'order' => "sort_order asc"
        )); //找出所有商城的合作伙伴
        $page['item_count'] = $this->_hotTheme_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('res', $res);
        $this->assign('filtered', $filter? 1 : 0); //是否有查询条件
        $page['item_count'] = $this->_hotTheme_mod->getCount();
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->display('hot_theme.index.html');
    }


    /**
     * 添加选中的主题
     */
    function add(){
        $page = $this->_get_page(10);   //获取分页信息

        $res = $this->_hotTheme_mod->find(); //找出所有热门主题
        $ids = '';
        foreach($res as $k=>$v){
            $ids .= $v['theme_id'].',';
        }
        $goods_ids = rtrim($ids,',');
        $verify = '';
        if($goods_ids){
            $verify = 'theme_id not in ('.$goods_ids.')';
        }
        $brands = $this->_theme_mod->find(array(
            'conditions' => $verify,
            'limit' => $page['limit'],
            'count' => true
        ));
        foreach ($brands as $key => &$ad) {
            $ad['theme_url'] = WEB_PHOTO_SRC . '/' . $ad['theme_url'];
        }
        $page['item_count'] = $this->_theme_mod->getCount();   //获取统计数据
        /* 导入jQuery的表单验证插件 */
        $this->import_resource(array(
            'script' => 'jqtreetable.js,inline_edit.js',
            'style' => 'res:style/jqtreetable.css'
        ));
        $this->_format_page($page);
        $this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
        $this->assign('res', $brands);
        $this->display('hot_theme.form.html');
    }

    /***选中*/
    function addPost(){
        $brand_ids = isset($_GET['theme_id']) ? trim($_GET['theme_id']) : '';
        if (!$brand_ids) {
            $this->show_warning('no_such_theme');
            return;
        }
//        $num = explode(',',$brand_ids);
//        $count = $this->_hotTheme_mod->getCount();
//        if($count > 2 || ($count+count($num)) > 2){
//            $this->show_warning('所选数量超过预设数量！');
//            return;
//        }

        $list = $this->_theme_mod->find(array(
            'conditions' => 'theme_id in ('.$brand_ids.')',
        ));
        //重组
        $newArr = array();
        foreach($list as $k=>$v){
            $newArr['theme_id'] = $v['theme_id'];
            $newArr['theme_title'] = $v['theme_title'];
            $brand_id = $this->_hotTheme_mod->add($newArr);
        }
        //添加数据
        if (!$brand_id) {
            $this->show_warning($this->_hotTheme_mod->get_error());
            return;
        } else {
            $this->admin_log("新增热门主题[" . $_POST['theme_name'] . "]", 1);
        }
        $this->show_message('添加成功', 'back_list', 'index.php?app=hot_theme', 'continue_add', 'index.php?app=hot_theme&amp;act=index'
        );
    }

    /***选中删除*/
    function drop() {
        $brand_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$brand_ids) {
            $this->show_warning('no_such_theme');
            return;
        }
        $brand_ids = explode(',', $brand_ids);
        $this->_hotTheme_mod->drop($brand_ids);
        if ($this->_hotTheme_mod->has_error()) {    //删除
            $this->show_warning($this->_hotTheme_mod->get_error());
            return;
        }else{
            $this->admin_log(Lang::get('del_brand'),1);
        }
        $this->show_message('删除成功');
    }

    //异步修改数据
    function ajax_col()
    {
        $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data   = array();

        if (in_array($column ,array('sort_order')))
        {
            $data[$column] = $value;
            $this->_hotTheme_mod->edit($id, $data);
            if(!$this->_hotTheme_mod->has_error())
            {
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return ;
        }
        return ;
    }

}